<template>
	<view >
		<hx-navbar  :border="false" :fixed="true" transparent="hidden"
			  height="140rpx">
			<view style="text-align: left;width: 100%;font-weight: 700;"><text>优惠券</text></view>
			
		</hx-navbar>
		<view style="background: linear-gradient(183.08deg, #DBF9F1 0%, rgba(219, 249, 241, 0) 46.82%), #FFFFFF;;height:140rpx;width: 100%;position: fixed;top: 0;"></view>
			<image src="../../../static/me/favorite.png" class="favorite" mode="scaleToFill" v-if="isflag"></image>
			<view v-else :class="from === 'pre' ? '' : container">
				<block v-for="(item,index) in couponList" :key="item.id" >		
					<view class="couponLi">
						<view class="detailed">
							<view class="left">
								<view class="discounts"><text>¥</text>{{item.amount}}</view>
								<!-- <view class="price">满{{item.price}}可用</view> -->
							</view>
							<view class="center">
								<view class="val">{{item.name}}</view>
								<view class="timeend">有效期至{{item.expirationTime
}}</view>
							</view>
							<view class="right">
								<view  v-if="from=='pre'">								
								<image src="/static/me/circle.png" v-if="couponId!==item.id" mode="scaleToFill" @click="useCoupon(item)"></image>
								<image src="/static/me/tick.png" v-else mode="scaleToFill" @click="useCoupon(item)" style="width: 60rpx;height: 60rpx;margin-left: 10rpx;"></image>
								</view>
								<view class="btn" v-else>去使用</view>
							</view>
						</view>
						<view class="rule">
							<view>使用规则</view>
							<u-icon name="arrow-down" color="#B2B0B0" size="20"></u-icon>
						</view>
					</view>
				</block>
			</view>
		<view class="footerBtn" v-if="from!=='pre' || isflag">
			<view class="left">优惠券历史记录</view>
			<view class="right">去领券中心看看</view>
		</view>
		<view class="confirm" v-else>
			<view class="left">可优惠<text>¥</text><text>{{totalYhPrice}}</text></view>
			<view class="right" @click="confirm">确认</view>
		</view>
	</view>
</template>

<script>
	export default{
		data(){
			return{
				isflag:false ,//有无优惠券，
				coupons:[
					{timeend:'2023.12.19',val:'新人专享券',price:'10',discounts:'3',id:1},
					{timeend:'2023.12.19',val:'新人专享券',price:'10',discounts:'4',id:2},
					{timeend:'2023.12.19',val:'新人专享券',price:'10',discounts:'6',id:3},
					{timeend:'2023.12.19',val:'新人专享券',price:'10',discounts:'12',id:4},
					{timeend:'2023.12.19',val:'新人专享券',price:'10',discounts:'7',id:5},
					{timeend:'2023.12.19',val:'新人专享券',price:'10',discounts:'9',id:6},
				],//模拟优惠券,
				imgsrc:'/static/me/circle.png',
				from:'',
				couponId:'',
				discounts:0,
				openid:'',
				totalYhPrice:0,
				couponList:[]
			}
		},
		onLoad(option){
			console.log(option)
			this.from = option.from
			this.openid = option.openid
			this.couponId=option.couponId
			this.getCouponList()
		},
		methods:{
			select(item){
				if(this.id === item.id){
					this.id=''
					this.discounts=0
				}else{
					
				this.id = item.id
				this.discounts=item.discounts
				}
			},
			//优惠券
			getCouponList() {
				let that = this
				uni.showLoading({
					title: "正在获取。。。"
				})
				uni.request({
					url: "https://zback.shixiaozhu.work/ShixiaozhuTest/coupon/showMyCoupons?openid=" + that.openid,
					success(res) {
						console.log(res)
						if (res.data.status == 200) {
							that.couponList = res.data.data
							// console.log("目前的couponList", that.couponList[0].amount)
							uni.hideLoading()
						}
					}
				})
			},
			useCoupon(item) {
				//已选择变成3
				if(this.couponId === item.id){
					this.couponId=''
					this.totalYhPrice=0
					item.status = 0
				}else{
					
				this.couponId = item.id
				this.totalYhPrice=item.amount
				item.status = 3
				}
				// for (let i = 0; i < this.couponList.length; i++) {
				// 	if (index == i) {
				// 		this.couponId = this.couponList[i].id
				// 			if(this.couponId === this.couponList[i].id){
				// 				this.couponId=''
				// 				this.totalYhPrice=0
				// 			}else{
								
				// 			this.couponId = this.couponList[i].id
				// 			this.totalYhPrice=this.totalYhPrice
				// 			}
				// 		console.log("优惠券id", this.couponId)
				// 		this.totalYhPrice = this.couponList[i].amount
				// 		// this.totalPrice = Number(this.totalPrice - this.totalYhPrice).toFixed(2)
				// 		this.couponList[i].status = 3
				// 	} else {
				// 		if (this.couponList[i].status == 3) {
				// 			this.couponList[i].status = 0
				// 		}
				// 	}
				// }
			},
			confirm(){
				// uni.$emit('returnData', this.totalYhPrice)
				let pages = getCurrentPages();  //获取跳转的所有页面
				let nowPage = pages[ pages.length - 1]; //当前页
				let prevPage = pages[ pages.length - 2 ]; //上一页
				prevPage.$vm.totalYhPrice=this.totalYhPrice 
				prevPage.$vm.couponId=this.couponId
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style lang="scss">
	page{
		background-color: rgba(243, 244, 246, 1);
	}
	.container{
		width: 100%;
		overflow-y: auto;
		position: absolute;
		top: 140rpx;
		bottom: 184rpx;
	
	}
	.favorite{
		width: 544rpx;
		height: 578rpx;
		position: absolute;
		top: 50%;
		left: 50%;
		transform: translate(-50%,-50%);
	}
	.footerBtn{
		position: fixed;
		bottom: 0;
		width: 100%;
		background-color: #fff;
		border-radius: 16rpx 16rpx 0 0;
		height: 164rpx;
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
		view{
			box-sizing: border-box;
			width: 324rpx;
			height: 100rpx;
			border: 2rpx solid #D9D9D9;
			border-radius: 14rpx;
			font-size: 30rpx;
			font-weight: 700;
			text-align: center;
			line-height: 100rpx;
			color: #000;
		}
		.right{
			border: 0;
			background: linear-gradient(102.81deg, #FC9A8D 31.27%, #FDDBB4 120.27%);
			color: #fff;
		}
		
	}
	.couponLi{
		margin: 24rpx 24rpx 0 24rpx ;
		padding: 22rpx;
		height: 282rpx;
		background-color: #fff;
		border-radius: 16rpx;
		display: flex;
		flex-direction: column;
		
		.detailed{
			border-bottom: 0.8rpx solid rgba(217, 217, 217, 1);
			flex: 3;
			display: flex;
			flex-direction: row;
			.left{
				flex: 1;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				.discounts{
					color: #F45B5B;
					font-size: 34rpx;
					font-weight: 700;
					letter-spacing: 8rpx;
					text{
						font-size: 16rpx;
					}
					margin-bottom: 10rpx;
				}
				.price{
					font-size: 20rpx;
					color: #666;
				}
			}
			.center{
				flex: 3;
				display: flex;
				flex-direction: column;
				justify-content: center;
				margin-left: 20rpx;
				.val{
					color: #000;
					font-weight: 700;
					margin-bottom: 10rpx;
				}
				.timeend{
					font-size: 20rpx;
					color: #666;
				}
			}
			.right{
				flex: 1;
				display: flex;
				justify-content: center;
				align-items: center;
				margin-left: -20rpx;
				image{
					width: 40rpx;
					height: 40rpx;
				}
				.btn{
					width: 128rpx;
					height: 52rpx;
					text-align: center;
					line-height: 52rpx;
					font-weight: 700;
					font-size: 24rpx;
					color: #fff;
					background: linear-gradient(180deg, #FC9A8D 0%, #FE9271 100%);
					border-radius: 16rpx;
				}
			}
		}
		.couponLi:last-child{
			margin-bottom: 184rpx;
		}
		.rule{
			flex: 1;
			// background-color: #000;
			display: flex;
			flex-direction: row;
			justify-content: space-between;
			align-items: center;
			font-size: 20rpx;
			color: #B2B0B0;
		}
		
	}
	.confirm{
		width: 94%;
		margin-left: 24rpx;
		height: 100rpx;
		position: fixed;
		bottom: 32rpx;
		display: flex;
		flex-direction: row;
		border-radius: 16rpx;
		overflow: hidden;
		color: #fff;
		.left{
			flex: 1;
			box-sizing: border-box;
			background: #404040;
			font-weight: 700;
			line-height: 100rpx;
			padding-left: 48rpx;
			font-size: 20rpx;
			text:nth-of-type(1){
				font-size: 16rpx;
				margin:0 4rpx 0 14rpx;
			}
			text:nth-of-type(2){
				font-size: 32rpx;
			}
		}
		.right{
			width: 204rpx;
			// flex: 1;
			font-size: 28rpx;
			font-weight: 700;
			text-align: center;
			line-height: 100rpx;
			background: linear-gradient(111.77deg, #FC9A8D 56.92%, #FDDBB4 109.99%);
			
		}
	}
</style>